package leetcode.周赛.第273场;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/12/26 11:34
 * @description：
 */
public class 执行所有后缀指令 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.executeInstructions(3, new int[]{0, 1}, "RRDDLU")));
    }


    class Solution {
        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        public int[] executeInstructions(int n, int[] startPos, String s) {
            int ans[] = new int[s.length()];
            for (int i = 0; i < s.length(); i++) {
                int curR = startPos[0];
                int curC = startPos[1];
                int j = i;
                for (; j < s.length(); j++) {
                    char c = s.charAt(j);
                    int idx = 0;
                    switch (c) {
                        case 'U' :
                            idx = 0;
                            break;
                        case 'D' :
                            idx = 1;
                            break;
                        case 'L' :
                            idx = 2;
                            break;
                        case 'R' :
                            idx = 3;
                            break;
                    }
                    curR += dirs[idx][0];
                    curC += dirs[idx][1];
                    if (curR < 0 || curR >= n || curC < 0 || curC >= n) {
                        break;
                    }
                }
                ans[i] = j - i;
            }
            return ans;
        }
    }
}
